const qiniuUploader = require('./qiniuUploader.js')

const upLoadQiNiu = () => new Promise((resolve, reject) => {
  let baseUrl = global.qiniuPath + '/'
  wx.chooseImage({
    count: 1,
    success: res => {
      let filePath = res.tempFilePaths[0];
      // 交给七牛上传
      qiniuUploader.upload(filePath, ({ key, hash }) => {
        resolve(baseUrl + key)
      }, (error) => {
        console.error('error: ' + JSON.stringify(error));
      });
    }
  })
})
//滚动分页
const sildePage = async (page_index, page_size, total_count) => {
  if (page_index * page_size >= total_count) {
    wx.showToast({
      title: '滑到底啦',
      icon: 'none'
    })
    return false
  } else {
    return ++page_index
  }
}

// 轮播详情 返回页面名称
/**
 * 
 * 跳转值类型定义(指定界面跳转->1: vip列表  2：探索中国列表  3:满优惠列表  4：养生礼包列表)  " +
        "(指定数据跳转->1:满优惠活动类型 2:爆单王活动类型 3:养生礼包数据类型 4:稀缺预售数据类型 5:探索中国数据类型 6:商品一级分类数据类型 7:商品二级分类数据类型 8:指定商品
 */
const bannerDetailUrl = async data => {
  // 1:指定界面跳转 2:指定数据跳转 
  let url = ''
  if (data.jumpType == 1) {
    switch (data.jumpValueType) {
      case 1: // vip
        url = 'vip'
        break
      case 2: // 探索中国
        url = 'exploreChina'
        break
      case 3: // 蛮优惠
        url = 'fullDisconut'
        break
      case 4: // 礼包
        url = 'giftPackages'
        break
    }
    return url
  }

  if (data.jumpType == 2) {
    switch (data.jumpValueType) {
      case 1: // 满优惠 homeid
        url = 'fullDisconut'
        if (data.name) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.name}`
        }
        if (data.adName) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.adName}`
        }
        if (data.popUpsName) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.popUpsName}`
        }
        break
      case 2: //  爆单王 infoids
        url = 'blastGoods'
        break
      case 3: // 养生礼包详情 id
        url = 'giftPackagesDetail'
        break
      case 4: // 稀缺预售 预售id
        url = `getSetBuy`
        break
      case 5: // 探索中国 infoids
        url = `exploreChinaDetail`
        break
      case 6: // 一级商品分类
        url = `firstClass`
        if (data.name) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.name}`
        }
        if (data.adName) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.adName}`
        }
        break
      case 7: // 二级商品分类
        url = `secondClass`
        if (data.name) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.name}`
        }
        if (data.adName) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.adName}`
        }
        if (data.popUpsName) {
          return `${url}?id=${data.jumpValue}&h=${true}&name=${data.popUpsName}`
        }
        break
      case 8: // 商品详情
        url = `goodsDetail`
        break
    }
    return `${url}?id=${data.jumpValue}&h=${true}`
  }
}

const sum = arr => {
  return arr.reduce((prev, curr, idx, arr) => prev + curr)
}
const format = (endTime) => {
  // let now = new Date();
  // let endDate = new Date(endTime);
  // let leftTime = endTime
  // if (leftTime < 0) return false
  // let leftsecond = parseInt(leftTime / 1000);
  // let day = Math.floor(leftsecond / (60 * 60 * 24));
  // let hour = Math.floor((leftsecond - day * 24 * 60 * 60) / 3600);
  // let minute = Math.floor((leftsecond - day * 24 * 60 * 60 - hour * 3600) / 60);
  // return `${day}天${hour}时${minute}分`
  var time = new Date(endTime);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return y + '-' + add0(m) + '-' + add0(d)
}
function add0(m) { return m < 10 ? '0' + m : m }
export default {
  upLoadQiNiu,
  sildePage,
  bannerDetailUrl,
  sum,
  format
}